package com.dis.service.service.impl;

import com.dis.common.constant.IsDeleteConstant;
import com.dis.common.constant.MessageConstant;
import com.dis.pojo.dto.ChronicDiseaseDTO;
import com.dis.pojo.entity.ChronicDisease;
import com.dis.pojo.entity.ChronicDiseaseTypeRelation;
import com.dis.common.exception.BaseException;
import com.dis.service.mapper.ChronicDiseaseMapper;
import com.dis.service.mapper.ChronicDiseaseTypeMapper;
import com.dis.service.mapper.ChronicDiseaseTypeRelationMapper;
import com.dis.pojo.queryDTO.ChronicDiseasePageQueryDTO;
import com.dis.common.result.PageResult;
import com.dis.service.service.ChronicDiseaseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
@RequiredArgsConstructor
public class ChronicDiseaseServiceImpl implements ChronicDiseaseService {

    @Autowired
    private ChronicDiseaseMapper chronicDiseaseMapper;

    private final ChronicDiseaseTypeMapper chronicDiseaseTypeMapper;

    private final ChronicDiseaseTypeRelationMapper chronicDiseaseTypeRelationMapper;

    /**
     * 新增慢性病
     * @param chronicDiseaseDTO
     * @return
     */
    @Override
    public void save(ChronicDiseaseDTO chronicDiseaseDTO) {
        ChronicDisease chronicDisease = new ChronicDisease();
        BeanUtils.copyProperties(chronicDiseaseDTO, chronicDisease);

        // 设置默认状态和创建时间
        chronicDisease.setIsDeleted(IsDeleteConstant.NOT_DELETED);
        chronicDisease.setCreateTime(LocalDateTime.now());
        chronicDisease.setUpdateTime(LocalDateTime.now());
        // 添加到慢性病表中,并且返回添加的主键
        chronicDiseaseMapper.insert(chronicDisease);
        // 添加联系到联系表里
        //使用build构建对象
        ChronicDiseaseTypeRelation relation = ChronicDiseaseTypeRelation.builder()
                .diseaseId(chronicDisease.getId())
                .typeId(chronicDiseaseDTO.getTypeId())
                .createTime(LocalDateTime.now()).build();
        chronicDiseaseTypeRelationMapper.insert(relation);
    }

    @Override
    public PageResult pageQuery(ChronicDiseasePageQueryDTO chronicDiseasePageQueryDTO) {
        PageHelper.startPage(chronicDiseasePageQueryDTO.getPage(), chronicDiseasePageQueryDTO.getPageSize());
        Page<ChronicDisease> page = chronicDiseaseMapper.pageQuery(chronicDiseasePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        ChronicDisease chronicDisease = ChronicDisease.builder()
                .id(id)
                .isDeleted(status)
                .updateTime(LocalDateTime.now())
                .build();
        chronicDiseaseMapper.update(chronicDisease);
    }
    /**
     * 根据ID查询慢性病
     * @param id
     * @return
     */
    @Override
    public ChronicDisease getById(Long id) {
        ChronicDisease chronicDisease = chronicDiseaseMapper.getById(id);
        if (chronicDisease == null) {
            throw new BaseException(MessageConstant.DISEASE_NOT_FOUND);
        }
        return chronicDisease;
    }

    @Override
    public void update(ChronicDiseaseDTO chronicDiseaseDTO) {
        ChronicDisease chronicDisease = new ChronicDisease();
        BeanUtils.copyProperties(chronicDiseaseDTO, chronicDisease);
        chronicDisease.setUpdateTime(LocalDateTime.now());
        chronicDiseaseMapper.update(chronicDisease);
        // 还要更新联系表
        chronicDiseaseTypeRelationMapper.updateByDiseaseId(chronicDiseaseDTO.getId(),chronicDiseaseDTO.getTypeId());
    }

    @Override
    public void deleteById(Long id) {
        // 逻辑删除，设置is_deleted为1
        chronicDiseaseMapper.deleteById(id);
    }

    /**
     * 根据病种id查询所属的病种种类信息
     */
    @Override
    public Object selectTypeInfo(Long id) {
        // 查询种类id
        Long typeId = chronicDiseaseMapper.getTypeId(id);
        return chronicDiseaseTypeMapper.selectTypeInfo(typeId);
    }
}
